package org.afs.gescol.core.dao.implementations;

import java.util.List;

import org.afs.gescol.core.dao.interfaces.LevelDao;
import org.afs.gescol.core.dto.LevelCriteria;
import org.afs.gescol.core.model.references.Level;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;

/**
 * Implementation of LevelDao.
 * @autor Kobla GBENYO (kobla.gbenyo@gmail.com)
 */
public class LevelDaoImpl extends HibernateDaoBaseImplementation implements LevelDao {

    @Override
    public Level getLevelById(Long id) {
        return load(id, Level.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Level> getLevelsByCriteria(LevelCriteria levelCriteria) {
        final Criteria criteria = createCriteria(Level.class);
        if (levelCriteria.getLabel() != null && !"".equals(levelCriteria.getLabel())) {
            criteria.add(Restrictions.or(
                Restrictions.ilike("shortLabel", levelCriteria.getLabel(), MatchMode.ANYWHERE),
                Restrictions.ilike("longLabel", levelCriteria.getLabel(), MatchMode.ANYWHERE)
            ));
        }
        if (levelCriteria.getCycleId() != null) {
            criteria.add(Restrictions.eq("levelCycle.id", levelCriteria.getCycleId()));
        }
        criteria.addOrder(Order.asc("shortLabel"));
        return criteria.list();
    }

}
